/* poporcc3.p -  PO RECEIPT CREATE RETAINED TAX TRGL_DET                   */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: poporcc3.p 54213 2015-04-09 02:10:35Z kbh                         $: */
/*                                                                         */
/*-------------------------------------------------------------------------
  Purpose: This program creates the posting of retained taxes
           on purchase order receipts, returns, and consigned usage.

  Notes:
---------------------------------------------------------------------------*/
/*                                                                         */
/* Revision: 1.1      BY: Patrick Rowan      DATE: 04/24/07  ECO: *R0C6* */
/* Revision: 1.4      BY: Neil Curzon        DATE: 09/01/09  ECO: *R1NG* */
/* Revision: 1.5      BY: Antony LejoS       DATE: 01/06/10  ECO: *R1V2* */
/* Revision: 1.6      BY: Huaping Luo        DATE: 03/16/10  ECO: *R21W* */
/* Revision: 1.7      BY: Neil Curzon        DATE: 08/03/10  ECO: *R24G* */
/* Revision: 1.9      BY: Huaping Luo        DATE: 08/06/10  ECO: *R24L* */   
/* Revision: 1.10     BY: Huaping Luo        DATE: 08/13/10  ECO: *R251* */
/*-Revision end------------------------------------------------------------*/

{us/bbi/mfdeclre.i}

{us/px/pxmaint.i}
{us/px/pxphdef.i ictrxr}

/* INPUT PARAMETERS */
define input parameter ip_receivernbr like prh_receiver no-undo.
define input parameter ip_po_nbr   like po_nbr no-undo.
define input parameter ip_pod_line  like pod_line no-undo.
define input parameter ip_last_sr_wkfl as logical no-undo.
define input parameter ip_accum_taxamt like tx2d_tottax no-undo.
define input parameter ip_totl_qty_this_rcpt like pod_qty_chg no-undo.
define input parameter ip_dr_proj as character no-undo.
define input parameter ip_cr_acct as character no-undo.
define input parameter ip_cr_sub  as character no-undo.
define input parameter ip_cr_cc as character no-undo.
define input parameter ip_cr_proj as character no-undo.
define input parameter ip_tr_trnbr like tr_trnbr no-undo.
define input parameter ip_trqty like tr_qty_chg no-undo.
define input parameter ip_po_curr like po_curr no-undo.
define input parameter ip_entity like si_entity no-undo.
define input parameter ip_same-ref as logical no-undo.
define input parameter using_supplier_consignment like mfc_logical no-undo.
define input parameter ip_consign_flag like mfc_logical no-undo.
define input parameter ip_is_usage     like mfc_logical no-undo.

/* VARIABLES */
define variable mc-error-number like msg_nbr no-undo.
{us/po/porcdef.i}

define shared variable pod_entity    like si_entity.
define shared variable pod_po_entity like si_entity.
define shared variable undo_all      like mfc_logical no-undo.

define variable l_ico-acct    as   character no-undo.
define variable l_ico-sub     as   character no-undo.
define variable l_ico-cc      as   character no-undo.
define variable l_podsite     like pod_site  no-undo.
define variable cIntercoRef   like glt_interco_ref no-undo.
define variable cInitialIntercoRef like glt_interco_ref no-undo.

define variable mgmt_acct_ex_rate1     as decimal       no-undo.
define variable mgmt_acct_ex_rate2     as decimal       no-undo.
define variable mgmttaxamt             like glt_amt no-undo.
define shared variable mgmt_curr              like glt_curr no-undo.
define variable mgmt_rate_type         as character no-undo.
define shared variable lInventIsFallBack      as logical no-undo.
define shared variable lStatIsFallBack        as logical no-undo.
define variable lStatCurrError  as logical no-undo.
define variable iStatErrorMsg   as integer no-undo.

{us/gp/gprunpdf.i "gpscpl" "p"}

/* trgl_det account variables */
{us/ic/icglacct.i}

{us/px/pxpgmmgr.i}

/*TO ASSIGN OID*/
{us/gp/gpoidfcn.i}

/* WORKFILE */
define workfile taxdetail
   field taxacct   like gl_ap_acct
   field taxsub    like gl_ap_sub
   field taxcc     like gl_ap_cc
   field taxamt    like tx2d_tottax.

cInitialIntercoRef = string(nextOidValue()).

for first gl_ctrl
   where gl_domain = global_domain
no-lock: end.

for each taxdetail exclusive-lock:
   delete taxdetail.
end.

{us/gp/gprunp.i "gpscpl" "p" "getStatutoryCurrency"
	"(input mgmt_curr,
	  input lStatIsFallBack,
	  input eff_date,
	  output mgmt_acct_ex_rate1,
	  output mgmt_acct_ex_rate2,
	  output mgmt_rate_type,
	  output lStatCurrError,
	  output iStatErrorMsg)"}

if lStatCurrError = true then do:
   {us/bbi/pxmsg.i &MSGNUM=iStatErrorMsg &ERRORLEVEL=4}
   undo_all = true.
   return.
end.

for each tx2d_det
    where tx2d_domain = global_domain and
          tx2d_ref = ip_receivernbr and
          tx2d_nbr = ip_po_nbr      and
          tx2d_tr_type = tax_tr_type and
          tx2d_line = ip_pod_line   and
          tx2d_cur_abs_ret_amt <> 0
no-lock break by tx2d_line:

   /*  FIND TAX MASTER TO GET ACCOUNTS */
   for first tx2_mstr
       where tx2_domain = global_domain and
             tx2_tax_code = tx2d_tax_code
   no-lock: end.

   /* ACCRUE TAX AT RECEIPT ONLY WHEN DEALING WITH PURCHASE RECEIPTS */
   /* AND CONSIGNED MATERIAL USAGES (RCT-PO TRANSACTIONS). DO NOT    */
   /* ACCRUE TAX AT RECEIPT WHEN PHYSICALLY RECEIVING CONSIGNED      */
   /* MATERIAL (CN-RCT TRANSACTIONS).                                */

   if available tx2_mstr
      and
      /* PURCHASE RECEIPT */
      (tx2d_rcpt_tax_point) and
      (not using_supplier_consignment or not ip_consign_flag)
      or
      /* CONSIGNMENT USAGE */
      (using_supplier_consignment and
       ip_consign_flag            and
       tx2_usage_tax_point        and
       ip_is_usage)
   then do:

      find first taxdetail
         where taxacct = tx2_apr_acct
         and taxsub = tx2_apr_sub
         and taxcc = tx2_apr_cc
      no-error.

      if not available taxdetail then do:

         create taxdetail.
         assign
            taxacct = tx2_apr_acct
            taxsub  = tx2_apr_sub
            taxcc   = tx2_apr_cc.

         recno = recid(taxdetail).

         if ip_last_sr_wkfl then
            assign taxamt = taxamt + tx2d_cur_abs_ret_amt.

         /* TO PREVENT ROUNDING ERRORS, THE LAST MULTI-ENTRY
            TRANSACTION WILL PLUG THE REMAINING TAX THAT HAS NOT
            BEEN ASSIGNED TO OTHER TRANSACTIONS. */
         if ip_last_sr_wkfl then do:
            if taxamt <> 0 and last-of(tx2d_line) then
               taxamt = taxamt - ip_accum_taxamt.
         end. /* IF LAST_SR_WKFL THEN */

         else
         if ip_totl_qty_this_rcpt = 0 then
            taxamt  = tx2d_cur_abs_ret_amt.

         else do:
            taxamt  = tx2d_cur_abs_ret_amt * (ip_trqty / ip_totl_qty_this_rcpt).
            /* ROUND PER BASE CURRENCY ROUND METHOD */
            {us/gp/gprunp.i "mcpl" "p" "mc-curr-rnd"
               "(input-output taxamt,
                 input gl_rnd_mthd,
                 output mc-error-number)"}
         end.

      end.

      else do:

         if ip_last_sr_wkfl then
            taxamt = taxamt + tx2d_cur_abs_ret_amt.

         /* TO PREVENT ROUNDING ERRORS, THE LAST MULTI-ENTRY
            TRANSACTION WILL PLUG THE REMAINING TAX THAT HAS NOT
            BEEN ASSIGNED TO OTHER TRANSACTIONS. */

         if ip_last_sr_wkfl then do:
            if taxamt <> 0 and last-of(tx2d_line) then
               taxamt = taxamt - ip_accum_taxamt.
         end. /* IF LAST_SR_WKFL THEN */

         else
         if ip_totl_qty_this_rcpt = 0 then
            taxamt = taxamt + tx2d_cur_abs_ret_amt.

         else do:
            taxamt =  taxamt + tx2d_cur_abs_ret_amt * (ip_trqty / ip_totl_qty_this_rcpt).
            recno = recid(taxdetail).
            /* ROUND PER BASE CURRENCY ROUND METHOD */
            {us/gp/gprunp.i "mcpl" "p" "mc-curr-rnd"
               "(input-output taxamt,
                 input gl_rnd_mthd,
                 output mc-error-number)"}
         end.  /* else do */

      end. /* IF AVAILABLE TAXDETAIL */

      if last-of(tx2d_line) then
         ip_accum_taxamt = ip_accum_taxamt + taxamt.

   end.  /* if available tx2_mstr and tx2d_rcpt_tax_point */

end.  /* for each tx2d_det */

for first pod_det
   where  pod_domain = global_domain
      and pod_nbr    = ip_po_nbr
      and pod_line   = ip_pod_line
no-lock: end.

if available pod_det
then do:

   /* RETRIEVE EXPENSED ITEM RECEIPTS ACCOUNT, SUB-ACCOUNT AND        */
   /* COST CENTER FOR ORDER CREATED IN CURRENT DATABASE               */
   for first pt_mstr
      where pt_domain = global_domain
      and   pt_part   = pod_part
   no-lock:

      /* RETREIVE THE EXPENSED ITEM RECEIPTS ACCOUNT, SUB-ACCOUNT AND */
      /* COST CENTER WHEN pod_type is "M"                             */
      if pod_type = "m"
      then do:
         if available gl_ctrl then
            assign
               ip_cr_acct = gl_rcptx_acct
               ip_cr_sub  = gl_rcptx_sub
               ip_cr_cc   = gl_rcptx_cc.
      end. /* IF pod_type = "m" */
      else do:

         for first po_mstr
            where po_domain = global_domain
            and   po_nbr    = ip_po_nbr
         no-lock:
         end. /* FOR FIRST po_mstr */

         if available po_mstr
         then do:
            for first vd_mstr
               where vd_domain = global_domain
               and   vd_addr   = po_vend
            no-lock:
            end. /* FOR FIRST vd_mstr */
         end. /* IF AVAILABLE po_mstr */

         /* RETRIEVE THE PURCHASE RECEIPTS ACCOUNT, SUB-ACCOUNT */
         /* AND COST CENTER                                     */
         {us/bbi/gprun.i ""glactdft.p""
            "(input  ""PO_RCPT_ACCT"",
              input  pt_mstr.pt_prod_line,
              input  pod_det.pod_site,
              input  if available vd_mstr
                     then
                        vd_type
                     else
                        """",
              input  """",
              input  no,
              output ip_cr_acct,
              output ip_cr_sub,
              output ip_cr_cc)"}

      end. /* ELSE DO */

   end. /* FOR FIRST pt_mstr */

end. /* IF AVAILABLE pod_det */

for each taxdetail
   where taxamt <> 0:

   /*  MOVED CODE FOR CREATION OF  trgl_det  AND glt_det RECORDS INTO THE  */
   /*  INTERNAL PROCEDURE "createGlRecords" TO FACILITATE REUSABILITY IN   */
   /*  MULTIENTITY SCENARIO                                               */
   run createGlRecords
      (input taxacct,
       input taxsub,
       input taxcc,
       input ip_cr_acct,
       input ip_cr_sub,
       input ip_cr_cc,
       input taxamt,
       input ip_entity).

   /* RETRIEVE INTER-COMPANY ACCT, SUBACCT AND CC FOR CR IN  */
   /* CURRENT DATABASE                                       */
   if pod_entity <> pod_po_entity
   then do:

      /* RETRIEVE INTER-COMPANY CR ACCOUNT */
      {us/gl/glenacex.i &entity = pod_entity
          &type   = '"CR"'
          &module = '"IC"'
          &acct   = l_ico-acct
          &sub    = l_ico-sub
          &cc     = l_ico-cc }

      cIntercoRef = cInitialIntercoRef.

      run createGlRecords
         (input ip_cr_acct,
          input ip_cr_sub,
          input ip_cr_cc,
          input l_ico-acct,
          input l_ico-sub,
          input l_ico-cc,
          input taxamt,
          input pod_entity).

      /* RETRIEVE INTER-COMPANY ACCT, SUBACCT AND CC FOR DR IN  */
      /* CURRENT DATABASE                                       */

      /* RETRIEVE INTER-COMPANY DR ACCOUNT */
      {us/gl/glenacex.i &entity = pod_po_entity
             &type   = '"DR"'
             &module = '"IC"'
             &acct   = l_ico-acct
             &sub    = l_ico-sub
             &cc     = l_ico-cc }

      cIntercoRef = cInitialIntercoRef.

      run createGlRecords
         (input l_ico-acct,
          input l_ico-sub,
          input l_ico-cc,
          input ip_cr_acct,
          input ip_cr_sub,
          input ip_cr_cc,
          input taxamt,
          input pod_po_entity).

   end. /* IF pod_entity <> pod_po_entity   */

end.  /* FOR EACH taxdetail */

PROCEDURE createGlRecords:
/* Purpose : PROCEDURE TO CREATE trgl_det AND glt_det RECORDS       */
/*------------------------------------------------------------------*/

   define input parameter p_acct1 as character      no-undo.
   define input parameter p_sub1  as character      no-undo.
   define input parameter p_cc1   as character      no-undo.
   define input parameter p_acct2 as character      no-undo.
   define input parameter p_sub2  as character      no-undo.
   define input parameter p_cc2   as character      no-undo.
   define input parameter p_taxamt like tx2d_tottax no-undo.
   define input parameter p_entity like si_entity   no-undo.

   define variable tempAmount like tx2d_tottax      no-undo.
   define buffer icc_ctrl for icc_ctrl.

   for first icc_ctrl where icc_ctrl.icc_domain = global_domain
      no-lock: end.

   assign tempAmount = p_taxamt
          mgmttaxamt = p_taxamt.

      if base_curr <> ip_po_curr
      then do:
         /* CONVERT TRGL_GL_AMT TO BASE CURRENCY */
         {us/gp/gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input  ip_po_curr,
              input  base_curr,
              input  exch_rate,
              input  exch_rate2,
              input  p_taxamt,
              input  true, /* DO ROUND */
              output tempAmount,
              output mc-error-number)"}.

          /* ROUND PER BASE CURRENCY ROUND METHOD */
         if mc-error-number <> 0
         then do:
            {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
         end. /* IF mc-error-number <> 0 */
      end.  /* IF base_curr <> ip_po_curr */
	  
      if ip_po_curr <> mgmt_curr
      then do:
         /* CONVERT BASE AMOUNT TO MANAGEMENT CURRENCY AMOUNT */
         {us/gp/gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input  base_curr,
              input  mgmt_curr,
              input  mgmt_acct_ex_rate1,
              input  mgmt_acct_ex_rate2,
              input  tempAmount,
              input  true, /* DO ROUND */
              output mgmttaxamt,
              output mc-error-number)"}
         if mc-error-number <> 0
         then do:
            {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
         end. /* IF mc-error-number <> 0 */
      end.

      for first tr_hist no-lock
         where tr_domain = global_domain
        and tr_trnbr  = ip_tr_trnbr:
      end.

       {us/px/pxrun.i &PROC  = 'createTrglDetRecord'
          &PROGRAM='ictrxr.p'
          &handle = ph_ictrxr
          &PARAM = "(input ip_tr_trnbr,
                     input transtype,
                     input p_acct1,
                     input p_sub1,
                     input p_cc1,
                     input ip_dr_proj,
                     input p_acct2,
                     input p_sub2,
                     input p_cc2,
                     input ip_cr_proj,
                     input tempAmount,
                     input yes, /*create glt_det */
                     input transtype, /*trans type*/
                     input ip_po_nbr, /* order #*/
                     input p_entity, /* entity */
                     input eff_date, /* effective date */
                     input ip_same-ref, /* summarize */
                     input icc_ctrl.icc_mirror, /* audit */
                     input recid(tr_hist),
                     input cIntercoRef, /* inter company ref# */
                     input mgmt_curr, /* currency */
                     input mgmt_acct_ex_rate1,    /* exchange rate */
                     input mgmt_acct_ex_rate2,    /* exchange rate 2 */
                     input mgmt_rate_type, /* exchange rate type */
                     input mgmttaxamt,    /* currency amount */
                     input-output ref, /* glt_ref */
                     output oidTrglDet
                   )"
          &NOAPPERROR=true
          &CATCHERROR=true}

END PROCEDURE. /* END OF createGlRecords */
